//  this library.
import 'package:flutter/material.dart';
import 'package:flutter_svg/flutter_svg.dart';
import 'package:hktk_app/dao/login_dao.dart';
import 'package:hktk_app/ienum/question/collect_type_enum.dart';
import 'package:hktk_app/ienum/question/question_type_enum.dart';
import 'package:hktk_app/model/question/paper_data_model.dart';
import 'package:hktk_app/util/data_util.dart';
import 'package:hktk_app/util/font_util.dart';
import 'package:hktk_app/util/my_util.dart';
import 'package:hktk_app/util/sz_util.dart';
import 'package:hktk_app/util/toast_util.dart';
import 'package:hktk_app/util/view_util.dart';

/// 题目页面标题组件
class StemTitleWidget extends StatefulWidget {
  /// 对应下标
  final int index;

  /// 试题状态
  final QuestionTypeEnum type;

  const StemTitleWidget({
    super.key,
    required this.index,
    required this.type,
  });

  @override
  State<StemTitleWidget> createState() => _StemTitleWidgetState();
}

class _StemTitleWidgetState extends State<StemTitleWidget> {
  late BuildContext _context;

  /// 是否收藏
  late CollectTypeEnum _stateCollect;

  /// 试题题型名称
  late String _typeName;

  @override
  void initState() {
    super.initState();
    debugPrint("stemTitleWidget 组件 -> initState 方法执行 ===> ");
    debugPrint("");
    DtkInfoModel dtk = dataUtilComm.paperData!.dtkList[widget.index];
    _stateCollect = dtk.collType;
    _typeName = dtk.type.desc;
  }

  @override
  Widget build(BuildContext context) {
    debugPrint("stemTitleWidget 组件 -> build 方法执行 ===> ");
    debugPrint("");
    _context = context;
    bool showCo = widget.type == QuestionTypeEnum.answer ||
        widget.type == QuestionTypeEnum.memory;
    return Row(
      mainAxisAlignment: MainAxisAlignment.spaceBetween,
      children: [
        _showType(),
        if (showCo) _showCollect(),
      ],
    );
  }

  /// 显示题型
  _showType() {
    return Container(
      padding: EdgeInsets.symmetric(vertical: 3, horizontal: 6),
      decoration: BoxDecoration(
        color: getColorBlue2(szYjms: szUtilComm.szInfo.yjms),
        borderRadius: BorderRadius.all(Radius.circular(4)),
      ),
      child: Text(
        _typeName,
        style: FontUtil.fontBlue1(16, szYjms: szUtilComm.szInfo.yjms),
      ),
    );
  }

  /// 显示收藏
  _showCollect() {
    return GestureDetector(
      onTap: _onTapFunSc,
      child: Row(
        mainAxisSize: MainAxisSize.min,
        children: [
          SvgPicture.asset(
            _stateCollect == CollectTypeEnum.suc
                ? "images/question/stem_title_star2.svg"
                : "images/question/stem_title_star.svg",
            width: 36,
            height: 36,
          ),
          hiSpace(width: 4),
          Text(
            "收藏",
            style: FontUtil.fontBlack1(20, szYjms: szUtilComm.szInfo.yjms),
          ),
        ],
      ),
    );
  }

  /// 收藏点击方法的
  _onTapFunSc() {
    debugPrint("stemTitleWidget 组件 -> 你点击了收藏 ===> ");
    // 判断是否处于游客模式
    if (visitorMode()) {
      // 游客模式，提示不能收藏
      ToastUtil.show(context: _context, msg: "游客模式不能收藏哦，请先登录");
      return;
    }
    // 请求收藏接
    DtkInfoModel dtk = dataUtilComm.paperData!.dtkList[widget.index];
    if (_stateCollect == CollectTypeEnum.def ||
        _stateCollect == CollectTypeEnum.cancel) {
      _stateCollect = CollectTypeEnum.suc;
      dtk.collData = DateTime.now().millisecondsSinceEpoch;
      dataUtilComm.paperData!.cc++;
    } else {
      _stateCollect = CollectTypeEnum.cancel;
      dtk.collData = null;
      dataUtilComm.paperData!.cc--;
    }
    dtk.collType = _stateCollect;
    dataUtilComm.paperData!.dtkList[widget.index] = dtk;
    setState(() {});
  }
}
